﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace project_phoenix.source
{
    class CharacterSelectScreen : GameScreen
    {
        // character bgs
        private Texture2D char_bg;

        private Texture2D player_ready;
        private Texture2D player_not_ready;


        // there can always be 1-4 players
        const int numPlayers = 4;
        const int selection_width = 320; // the width of each selection pane
        const int ready_y_coord = 500;

        // there are 4 points for the 4 chars
        Point[] char_bg_coords;
        Point[] ready_coords;
        bool[] players_ready;



        public CharacterSelectScreen()
        {
            char_bg_coords = new Point[numPlayers];
            players_ready = new bool[numPlayers];
            ready_coords = new Point[numPlayers];

            // get the bubble placing
            int ready_bubble_offset = selection_width;

            // setup our x coords
            for(int count = 0; count < numPlayers; count++)
            {
                // setup coords
                char_bg_coords[count].X = count * selection_width;
                char_bg_coords[count].Y = 0;

                // unhardcode this
                ready_coords[count].X = count * ready_bubble_offset + (80);
                ready_coords[count].Y = ready_y_coord;

                players_ready[count] = false; // nobody is ready to begin with
            }


        }

        public override void LoadContent()
        {
            // get our content manager
            ContentManager content = ScreenManager.Game.Content;


            // Load our bg stuff -- TODO : replace with animations later that look cool!
            char_bg = content.Load<Texture2D>("sprites\\charselect");
            player_ready = content.Load<Texture2D>("sprites\\ready");
            player_not_ready = content.Load<Texture2D>("sprites\\notReady");
        }

        public override void UnloadContent()
        {

        }


        public override void Draw(GameTime gameTime)
        {

            // Get our sprite batch
            ScreenManager screenManager = this.ScreenManager;

            GameSystems.Graphics.BeginSprites();

            //foreach (Point p in char_bg_coords)
            //{
            //    spriteBatch.Draw(char_bg, new Vector2(p.X, p.Y), Color.White);
            //}

            for (int i = 0; i < numPlayers; i++)
            {
                // draw the boxes
                GameSystems.Graphics.DrawImage(char_bg, new Vector2(char_bg_coords[i].X, char_bg_coords[i].Y), Color.White);

                // draw the ready / not ready bubbles
                if (players_ready[i] == true)
                {
                    GameSystems.Graphics.DrawImage(player_ready, new Vector2(ready_coords[i].X, ready_coords[i].Y), Color.White);
                }
                else
                {
                    GameSystems.Graphics.DrawImage(player_not_ready, new Vector2(ready_coords[i].X, ready_coords[i].Y), Color.White);
                }
            }

            GameSystems.Graphics.EndSprites();
            //spriteBatch.DrawString(m_spriteFont, m_text, m_position, color);
        }

        public override void HandleInput(InputState input)
        {
            // Accept or cancel the menu? We pass in our ControllingPlayer, which may
            // either be null (to accept input from any player) or a specific index.
            // If we pass a null controlling player, the InputState helper returns to
            // us which player actually provided the input. We pass that through to
            // OnSelectEntry and OnCancel, so they can tell which player triggered them.
            PlayerIndex playerIndex;

            if (input.IsMenuSelect(null, out playerIndex))
            {
                // check to make sure we dont start the game yet
                bool readyToStart = true;
                for (int i = 0; i < numPlayers; i++)
                {
                    // if the controller is not on, or the controll is on and ready this is an ok solution
                    if ((input.GamePadWasConnected[i] == false)|| (input.GamePadWasConnected[i] == true && players_ready[i] == true))
                    {
                        continue;
                    }
                    else
                    {
                        readyToStart = false;
                        break;
                    }
                }

                // we are ready to start the game -- go to the loading screen
                if (readyToStart == true)
                {
                    LoadingScreen.Load(ScreenManager, true, playerIndex, new GameplayScreen(null));
                }

                // ok, just set the player to ready
                OnReady(playerIndex);
            }
            else if (input.IsMenuCancel(null, out playerIndex))
            {
                OnCancel(playerIndex);
            }
        }


        protected virtual void OnReady(PlayerIndex playerIndex)
        {
            // there should be a better way to do this, but this works for now...
            switch (playerIndex)
            {
                case PlayerIndex.One:
                    players_ready[0] = true;
                    break;
                case PlayerIndex.Two:
                    players_ready[1] = true;
                    break;
                case PlayerIndex.Three:
                    players_ready[2] = true;
                    break;
                case PlayerIndex.Four:
                    players_ready[3] = true;
                    break;
            };
        }

        protected virtual void OnCancel(PlayerIndex playerIndex)
        {
            // there should be a better way to do this, but this works for now...
            switch (playerIndex)
            {
                case PlayerIndex.One:
                    {
                        if (players_ready[0] == true)
                            players_ready[0] = false;
                        else
                            ExitScreen();

                        break;
                    }
                case PlayerIndex.Two:
                    {
                        if (players_ready[1] == true)
                            players_ready[1] = false;
                        else
                            ExitScreen();
                        break;
                    }
                case PlayerIndex.Three:
                    {
                        if (players_ready[2] == true)
                            players_ready[2] = false;
                        else
                            ExitScreen();
                        break;
                    }
                case PlayerIndex.Four:
                    {
                        if (players_ready[3] == true)
                            players_ready[3] = false;
                        else
                            ExitScreen();
                        break;
                    }
            };
        }

    }
}
